﻿Option Strict On

Namespace FTP
    ''' <summary>
    ''' FTP response class containing the FTP raw text, response code, and other information.
    ''' </summary>
    Public Class FtpResponse
        Private _rawText As String
        Private _text As String
        Private _code As FtpResponseCode = FtpResponseCode.None
        Private _isInformational As Boolean

        ''' <summary>
        ''' Default constructor for FtpResponse.
        ''' </summary>
        Public Sub New()
        End Sub

        ''' <summary>
        ''' Constructor for FtpResponse.
        ''' </summary>
        ''' <param name="rawText">Raw text information sent from the FTP server.</param>
        Public Sub New(ByVal rawText As String)
            _rawText = rawText
            _text = ParseText(rawText)
            _code = ParseCode(rawText)
            _isInformational = ParseInformational(rawText)
        End Sub

        ''' <summary>
        ''' Constructor for FtpResponse.
        ''' </summary>
        ''' <param name="response">FtpResponse object.</param>
        Public Sub New(ByVal response As FtpResponse)
            _rawText = response.RawText
            _text = response.Text
            _code = response.Code
            _isInformational = response.IsInformational
        End Sub

        ''' <summary>
        ''' Get raw server response text information.
        ''' </summary>
        Public ReadOnly Property RawText() As String
            Get
                Return _rawText
            End Get
        End Property

        ''' <summary>
        ''' Get the server response text.
        ''' </summary>
        Public ReadOnly Property Text() As String
            Get
                Return _text
            End Get
        End Property

        ''' <summary>
        ''' Get a value indicating the FTP server response code.
        ''' </summary>
        Public ReadOnly Property Code() As FtpResponseCode
            Get
                Return _code
            End Get
        End Property

        Friend ReadOnly Property IsInformational() As Boolean
            Get
                Return _isInformational
            End Get
        End Property

        Private Function ParseCode(ByVal rawText As String) As FtpResponseCode
            Dim code As FtpResponseCode = FtpResponseCode.None

            If rawText.Length >= 3 Then
                Dim codeString As String = rawText.Substring(0, 3)
                Dim codeInt As Integer = 0

                If Int32.TryParse(codeString, codeInt) Then
                    code = DirectCast(codeInt, FtpResponseCode)
                End If
            End If

            Return code
        End Function

        Private Function ParseText(ByVal rawText As String) As String
            If rawText.Length > 4 Then
                Return rawText.Substring(4).Trim()
            Else
                Return String.Empty
            End If
        End Function

        Private Function ParseInformational(ByVal rawText As String) As Boolean
            If rawText.Length >= 4 AndAlso rawText(3) = "-"c Then
                Return True
            Else
                Return False
            End If
        End Function


    End Class
End Namespace